package com.xiaoba.shoppingcart.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.xiaoba.common.redis.service.RedisService;
import com.xiaoba.common.security.utils.SecurityUtils;
import com.xiaoba.shoppingcart.mapper.CartMapper;
import com.xiaoba.system.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xiaoba.common.log.annotation.Log;
import com.xiaoba.common.log.enums.BusinessType;
import com.xiaoba.common.security.annotation.RequiresPermissions;
import com.xiaoba.shoppingcart.domain.Cart;
import com.xiaoba.shoppingcart.service.ICartService;
import com.xiaoba.common.core.web.controller.BaseController;
import com.xiaoba.common.core.domain.Result;
import com.xiaoba.common.core.utils.poi.ExcelUtil;
import com.xiaoba.common.core.domain.Result;
import com.xiaoba.common.core.domain.Result;
import com.xiaoba.common.core.web.page.TableDataInfo;

/**
 * 购物车Controller
 *
 * @author gd
 * @date 2024-06-07
 */
@RestController
@RequestMapping("/cart")
public class CartController extends BaseController
{
    @Autowired
    private ICartService cartService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private CartMapper cartMapper;

    /**
     * 查询购物车列表
     */
    @RequiresPermissions("shoppingcart:cart:list")
    @GetMapping("/list")
    public Result<TableDataInfo> list(Cart cart)
    {
        startPage();
        List<Cart> list = cartService.selectCartList(cart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @RequiresPermissions("shoppingcart:cart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cart cart)
    {
        List<Cart> list = cartService.selectCartList(cart);
        ExcelUtil<Cart> util = new ExcelUtil<Cart>(Cart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @RequiresPermissions("shoppingcart:cart:query")
    @GetMapping(value = "/{shoppingcartId}")
    public Result getInfo(@PathVariable("shoppingcartId") Long shoppingcartId)
    {
        return success(cartService.selectCartByShoppingcartId(shoppingcartId));
    }

    /**
     * 新增购物车
     */
    @RequiresPermissions("shoppingcart:cart:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody Cart cart)
    {
        return toAjax(cartService.insertCart(cart));
    }

    /**
     * 修改购物车
     */
    @RequiresPermissions("shoppingcart:cart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody Cart cart)
    {
        return toAjax(cartService.updateCart(cart));
    }

    /**
     * 删除购物车
     */
    @RequiresPermissions("shoppingcart:cart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shoppingcartIds}")
    public Result remove(@PathVariable Long[] shoppingcartIds)
    {
        return toAjax(cartService.deleteCartByShoppingcartIds(shoppingcartIds));
    }


    /**
     * 添加购物车
     * @param skuId
     * @param num
     */
    @GetMapping("addCart/{skuId}/{num}")
    public void addCart(@PathVariable("skuId") Long skuId,@PathVariable("num") Integer num) {
        cartService.addCart(skuId, num);
    }

    /**
     * 删除购物车明细
     *  物理删除  0 删除  1  未删除
     */
    @GetMapping("updateStatusToCart/{shoppingcartId}")
    public Result updateStatusToCart(@PathVariable Long shoppingcartId){
        return cartService.updateStatusToCart(shoppingcartId);
    }


    /**
     * 获取缓存中的购物车
     */
    @GetMapping("/getCartRedis")
    public void getCartRedis() {
        Long loginUserId = SecurityUtils.getUserId();
        if (loginUserId != null) {
            Map<String, Object> cacheMap = redisService.getCacheMap(loginUserId.toString());
            for (Object value : cacheMap.values()) {
                Cart s = (Cart) value;
                Cart mallShopInfo = cartMapper.selectByuserId(s.getShoppingcartUserId());
                if (mallShopInfo == null) {
                    cartMapper.insertCart(s);
                }
            }
        }
    }
}
